<?php

namespace Acme\CommonBundle\Controller;

use Symfony\Component\Security\Core\SecurityContext;
use Acme\UserBundle\Controller\SecurityController;
use Acme\CommonBundle\Entity\Startimage;
// use Symfony\Component\Locale\Locale;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
// these import the "@Route" and "@Template" annotations
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

class CommonController extends Controller {
	private $message = "";
	private $_dir;
	private $_width = 300;
	
	/**
	 * @Route("/", name="_common_start")
	 * @Template()
	 */
	public function indexAction(Request $request) {
		
		// print_r($this->get('security.context')->getToken()->getUser()); exit;
		// print_r($this->get('security.context')->isGranted('ROLE_ADMIN')); exit;
		// if($this->get('security.context')->getToken()->getUser()){
		// return $this->redirect($this->generateUrl('login_index'));
		// }
		// $this->get('security.context')->isGranted('IS_AUTHENTICATED_FULLY');
		
		// echo $request->getUri(); exit;
		$session = $request->getSession ();
		// get the login error if there is one
		if ($request->attributes->has ( SecurityContext::AUTHENTICATION_ERROR )) {
			$error = $request->attributes->get ( SecurityContext::AUTHENTICATION_ERROR );
		} else {
			$error = $session->get ( SecurityContext::AUTHENTICATION_ERROR );
			$session->set ( SecurityContext::AUTHENTICATION_ERROR, '' );
		}
		
		$baseUrl = $request->getScheme () . '://' . $request->getHttpHost () . $request->getBasePath ();
		$this->_dir = $this->get ( 'kernel' )->getRootDir () . '/../web/media/image/startpage';
		$_baseUrl = $baseUrl . '/../media/image/startpage/thumbs';
		$arr_ObjstartImage = array ();
		if ($handle = opendir ( $this->_dir )) {
			$i = 1;
			while ( false !== ($file = readdir ( $handle )) ) {
				if ($file != "." && $file != ".." && ! is_dir ( $this->_dir . '/' . $file )) {
					$this->resizeImage ( $file );
					$startImage = new Startimage ();
					$startImage->setId ( $i );
					$startImage->setImage ( $_baseUrl . '/' . $file );
					$arr_ObjstartImage [] = $startImage;
					$i ++;
				}
			}
			closedir ( $handle );
		}
		
		shuffle ( $arr_ObjstartImage );
		
		return array (
				'startimage' => $arr_ObjstartImage,
				'error' => $error 
		);
	}
	private function resizeImage($filename) {
		list ( $width, $height ) = getimagesize ( $this->_dir . '/' . $filename );
		// calulate percent
		$percent = number_format ( ($this->_width / ($width / 100)) / 100, 2 );
		$newwidth = $width * $percent;
		$newheight = $height * $percent;
		$img = imagecreatetruecolor ( $newwidth, $newheight );
		$source = imagecreatefromjpeg ( $this->_dir . '/' . $filename );
		// Resize
		imagecopyresized ( $img, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height );
		if (! is_dir ( $this->_dir . '/thumbs' )) {
			mkdir ( $this->_dir . '/thumbs' );
		}
		imagejpeg ( $img, $this->_dir . '/thumbs/' . $filename, 100 );
	}
}
